#include "shared_ptr.h"
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
using namespace MySTL;
using namespace std;

class Book {
public:
  Book() { cout << "Book Construct" << endl; }
  ~Book() { cout << "Book Destroy" << endl; }
};
int main() {
  shared_ptr<Book> p1(new Book);
  std::cout << p1.use_count() << std::endl;
  // 赋值构造函数
  shared_ptr<Book> p2(p1);
  std::cout << p1.use_count() << std::endl;
  // 赋值运算符
  shared_ptr<Book> p3;
  p3 = p2;
  std::cout << p1.use_count() << std::endl;
  // 移动赋值运算符
  shared_ptr<Book> p4;
  p4 = std::move(p3);
  std::cout << p1.use_count() << std::endl;
  // 移动拷贝构造函数
  shared_ptr<Book> p5(std::move(p4));
  std::cout << p5.use_count() << std::endl;
  std::vector<thread> threads;
  // 并发测试
  for (int i = 0; i < 20; i++) {
    threads.emplace_back([&p5]() {
      shared_ptr<Book> p6 = p5;
      std::cout << p6.use_count() << std::endl;
      this_thread::sleep_for(std::chrono::seconds(4));
    });
  }
  for (auto &t : threads) {
    t.join();
  }

  return 0;
}